package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysArtistLab;
import com.ruoyi.system.domain.SysArtistProject;
import com.ruoyi.system.domain.SysProject;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.SysArtist;
import com.ruoyi.system.service.ISysArtistService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 艺人管理Service业务层处理
 * 
 * @author cooc
 * @date 2024-06-19
 */
@Service
public class SysArtistServiceImpl implements ISysArtistService 
{
    @Autowired
    private SysArtistMapper sysArtistMapper;
    @Autowired
    private SysArtistLabMapper sysArtistLabMapper;

    @Autowired
    private SysLabMapper sysLabMapper;

    @Autowired
    private SysArtistProjectMapper sysArtistProjectMapper;

    @Autowired
    private SysArtistProjectServiceImpl sysArtistProjectService;

    @Autowired
    private SysProjectMapper sysProjectMapper;

    /**
     * 查询艺人管理
     * 
     * @param id 艺人管理主键
     * @return 艺人管理
     */
    @Override
    public SysArtist selectSysArtistById(Long id)
    {
        SysArtist res = sysArtistMapper.selectSysArtistById(id);
        initSysArtist(res);
        return res;
    }

    public List<SysArtist> selectSysArtistByIds(List<Long> ids)
    {
        List<SysArtist> res = new ArrayList<>();
        if(ids!=null && !ids.isEmpty()){
            res = ids.stream().map(this::selectSysArtistById).collect(Collectors.toList());
        }
        return res;
    }

    /**
     * 查询艺人管理列表
     * 
     * @param sysArtist 艺人管理
     * @return 艺人管理
     */
    @Override
    public List<SysArtist> selectSysArtistList(SysArtist sysArtist)
    {
        List<SysArtist> res = filterBySysArtist(sysArtist);
        if(!res.isEmpty()){
            for (SysArtist item: res){
                initSysArtist(item);
            }
        }
        return res;
    }

    /**
     * 新增艺人管理
     * 
     * @param sysArtist 艺人管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysArtist(SysArtist sysArtist)
    {
        sysArtist.setCreateTime(DateUtils.getNowDate());
        int row = sysArtistMapper.insertSysArtist(sysArtist);
        insertRelations(sysArtist);
        return row;
    }

    /**
     * 修改艺人管理
     * 
     * @param sysArtist 艺人管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysArtist(SysArtist sysArtist)
    {
        sysArtist.setUpdateTime(DateUtils.getNowDate());
        //修改艺人没有修改项目自定义字段信息的功能了
//        insertRelations(sysArtist);
        //只单独调用更新艺人标签的方法
        insertLabs(sysArtist);
        return sysArtistMapper.updateSysArtist(sysArtist);
    }

    /**
     * 批量删除艺人管理
     * 
     * @param ids 需要删除的艺人管理主键
     * @return 结果
     */
    @Override
    public int deleteSysArtistByIds(Long[] ids)
    {
        sysArtistLabMapper.deleteSysArtistLabByArtistIds(ids);
        for (Long id : ids) {
           sysArtistProjectMapper.deleteSysArtistProjectByArtistId(id);
        }
        return sysArtistMapper.deleteSysArtistByIds(ids);
    }

    /**
     * 删除艺人管理信息
     * 
     * @param id 艺人管理主键
     * @return 结果
     */
    @Override
    public int deleteSysArtistById(Long id)
    {
        sysArtistLabMapper.deleteSysArtistLabByArtistId(id);
        sysArtistProjectMapper.deleteSysArtistProjectByArtistId(id);
        return sysArtistMapper.deleteSysArtistById(id);
    }


    private void insertRelations(SysArtist sysArtist){
        insertLabs(sysArtist);
        insertProjectInfo(sysArtist);
    }


    /**
     * 插入人员标签关联信息
     * @param sysArtist
     */
    private void insertLabs(SysArtist sysArtist){
        //先删关系再插入
        delLabs(sysArtist);
        Long[] labs = sysArtist.getLabs();
        if(labs!=null && labs.length>0){
            for (Long lab : labs) {
                SysArtistLab sysArtistLab = new SysArtistLab();
                sysArtistLab.setArtistId(sysArtist.getId());
                sysArtistLab.setLabId(lab);
                sysArtistLabMapper.insertSysArtistLab(sysArtistLab);
            }
        }
    }


    /**
     *删除人员标签关系
     * @param sysArtist
     */
    private void delLabs(SysArtist sysArtist){
        sysArtistLabMapper.deleteSysArtistLabByArtistId(sysArtist.getId());
    }


    /**
     * 插入人员关联的项目信息
     * @param sysArtist
     */
    private void insertProjectInfo(SysArtist sysArtist){
        // 不用删除，更新艺人信息不能修改艺人与项目关系了
        // delProjectInfo(sysArtist);
        List<SysArtistProject> list = sysArtist.getProjectInfoList();
        if(!list.isEmpty()){
            for (SysArtistProject item:list){
                //项目数据校验
                final SysProject sysProject = sysProjectMapper.selectSysProjectById(item.getProjectId());
                if(sysProject!=null){
                    //插入项目关联信息
                    item.setArtistId(sysArtist.getId());
                    sysArtistProjectMapper.insertSysArtistProject(item);
                }else {
                    throw new RuntimeException("项目中未找到项目ID为"+item.getProjectId()+"的项目信息");
                }
            }
        }
    }

    private void delProjectInfo(SysArtist sysArtist){
        sysArtistProjectMapper.deleteSysArtistProjectByArtistId(sysArtist.getId());
    }

    public void initSysArtist(SysArtist sysArtist){
        List<SysArtistLab> list = sysArtistLabMapper.selectSysArtistLabByArtistId(sysArtist.getId());
        if(!list.isEmpty()){
            Long[] labs  = sysArtistLabMapper.selectSysArtistLabByArtistId(sysArtist.getId()).stream()
                    .map(SysArtistLab::getLabId)
                    .toArray(Long[]::new);
            sysArtist.setLabs(labs);
            String[] labNames  = Arrays.stream(labs)
                    .map(item->sysLabMapper.selectSysLabById(item).getName())
                    .toArray(String[]::new);
            sysArtist.setLabNames(labNames);
        }
        List<SysArtistProject> projectInfoList = sysArtistProjectMapper.selectSysArtistProjectByArtistId(sysArtist.getId());
        if(!projectInfoList.isEmpty()){
           for (SysArtistProject item:projectInfoList){
               item.setProjectName(sysProjectMapper.selectSysProjectById(item.getProjectId()).getName());
           }
        }
        Long[]  projectIds = projectInfoList.stream().map(SysArtistProject::getProjectId).toArray(Long[]::new);
        sysArtist.setProjectIds(projectIds);
        sysArtist.setProjectInfoList(projectInfoList);
    }

    private List<SysArtist> filterByProjectId(List<SysArtist> list ,Long projectId){
        return list.stream().filter(item->item.getProjectIds()!=null && Arrays.asList(item.getProjectIds()).contains(projectId)).collect(Collectors.toList());
    }

    private List<SysArtist> filterByLabId(List<SysArtist> list ,Long labId){
        return list.stream().filter(item->item.getLabs()!=null && Arrays.asList(item.getLabs()).contains(labId)).collect(Collectors.toList());

    }

    private List<SysArtist> filterBySysArtist(SysArtist sysArtist){
        List<SysArtist> all =  sysArtistMapper.selectSysArtistList(sysArtist);
        initSysArtists(all);
        List<SysArtist> resLab = filterByLabIds(all,sysArtist.getLabs());
        return filterByProjectIds(resLab, sysArtist.getProjectIds());
    }

    private List<SysArtist> filterByProjectIds(List<SysArtist> list ,Long[] projectIds){
        List<SysArtist> res = new ArrayList<>();
        if(projectIds==null || projectIds.length==0){
            return list;
        }else {
            for (Long projectId : projectIds) {
                res.addAll(filterByProjectId(list, projectId));
            }
        }
        return res;
    }


    private List<SysArtist> filterByLabIds(List<SysArtist> list ,Long[] labIds){
        List<SysArtist> res = new ArrayList<>();
        if(labIds==null || labIds.length==0){
            return list;
        }else {
            for (Long labId : labIds) {
                res.addAll(filterByLabId(list, labId));
            }
        }
        return res;
    }


    private List<SysArtist> initSysArtists(List<SysArtist> sysArtistList){
        if(sysArtistList!=null && sysArtistList.size()>0){
            for (SysArtist item: sysArtistList){
                initSysArtist(item);
            }
        }
        return sysArtistList;
    }
}
